package com.orangelife.service.propFeeAccount.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.orangelife.exception.AppBizException;
import com.orangelife.redis.core.redis.session.redisSession.WebServiceSession;
import com.orangelife.service.propFeeAccount.IPropFeeAccountService;
import com.orangelife.service.propFeeAccount.model.PropFeeAccountDto;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

public class HandleAccountThread extends Thread implements Runnable {

	private String threadName;
	private List<Map<String, Object>> data;
	private int start;
	private int end;
	private String commID;

	private WebServiceSession session;

	private IPropFeeAccountService propFeeAccountService;
	private List<Map<String, Object>> houseInfos;

	public HandleAccountThread(String commID, String threadName,
			List<Map<String, Object>> data,
			IPropFeeAccountService propFeeAccountService,
			WebServiceSession session, List<Map<String, Object>> houseInfos) {
		this.threadName = threadName;
		this.data = data;
		this.propFeeAccountService = propFeeAccountService;
		this.commID = commID;
		this.session = session;
		this.houseInfos = houseInfos;

	}

	public void run() {
		try {
			List<PropFeeAccountDto> propFeeAccountDtos = new ArrayList<PropFeeAccountDto>();
			for (Map<String, Object> map : data) {
				map.put("commID", commID);
				List<Map<String, Object>> houseInfo = new ArrayList<Map<String, Object>>();// propFeeAccountService
				// .getApaptMentInfoByName(map);
				/***
				 * 匹配房屋信息
				 */
				for (Map<String, Object> house : houseInfos) {
					String unitName = map.get("unitName") != null ? map.get(
							"unitName").toString() : null;
					String buildingName = map.get("buildingName").toString();
					String apartmentName = map.get("apartmentName").toString();
					if (StringUtils.isBlank(unitName)) {
						if (house.get("buildingName").toString()
								.equals(buildingName)
								&& house.get("apartmentNum").toString()
										.equals(apartmentName)
								&& StringUtils.isBlank(StringUtils
										.toString(house.get("unitName")))) {
							houseInfo.add(house);
						}
					} else {
						if (house.get("buildingName").toString()
								.equals(buildingName)
								&& house.get("apartmentNum").toString()
										.equals(apartmentName)
								&& unitName.equals(house.get("unitName")
										.toString())) {
							houseInfo.add(house);
						}
					}
				}
				if (houseInfo != null && houseInfo.size() > 0) {
					String apartmentID = houseInfo.get(0).get("apartmentID")
							.toString();
					List<PropFeeAccountDto> propFeeAc = propFeeAccountService
							.findPropFeeAccountByRelevantID(null, apartmentID,
									"1",String.valueOf(map.get("account_type_code")));
					if (propFeeAc == null || propFeeAc.size() <= 0) {
						PropFeeAccountDto propFeeAccount = new PropFeeAccountDto();
						propFeeAccount.setAccountID(IdUtil.getuuId());
						propFeeAccount.setItemStat(TypeConst.FLAG_ON);
						propFeeAccount.setTscreate(new Date());
						propFeeAccount.setTsedit(new Date());
						propFeeAccount.setStatus("1");
						propFeeAccount.setApartmentID(apartmentID);
						propFeeAccount.setMoney(new BigDecimal(map.get("money")
								.toString()));
						propFeeAccount.setAccountTypeCode(String.valueOf(map.get("account_type_code")));
						propFeeAccount.setCreateuserID(session.getUserID());
						propFeeAccount.setCreateuserUser(StringUtils
								.isBlank(session.getRealname()) ? session
								.getTrueName() : session.getRealname());
						propFeeAccountDtos.add(propFeeAccount);
					}
				}

			}

			propFeeAccountService.addPropFeeAccount(propFeeAccountDtos);
		} catch (AppBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
